Rein.HomePage = function() {
    
    this.init = function() {
        
        this.folders = {};
        
        // Create folder listbox
        this.lbxFolderTree = new Rein.TreeListbox('#treeFolders');
        $(this.lbxFolderTree).on('select', $.proxy(this.handleFolderSelect, this));
        
        // Create table
        this.tblLinks = new Rein.ScrollableTable('#tblLinks', {
            headers: ['Name', 'Date created', 'Date modified'],
            height: 382
        });
        
        // Init buttons
        this.btnNewFolder = $('#btnNewFolder');
        this.btnNewFolder.on('click', $.proxy(function(){
            window.location = 'NewFolder';
        }, this));
        
        this.btnEditFolder = $('#btnEditFolder');
        this.btnEditFolder.on('click', $.proxy(function(){
            window.location = 'EditFolder';
        }, this));
        
        this.btnDeleteFolder = $('#btnDeleteFolder');
        this.btnDeleteFolder.on('click', $.proxy(function(){
            var dlg = new Rein.ConfirmDialog("Do you realy want to delete this folder?", $.proxy(function() {
                $.ajax({
                    url: 'DeleteFolder',
                    type: 'GET',
                    data: {
                        folder_id: this.lbxFolderTree.getSelectedValue()
                    },
                    complete: $.proxy(function(response) {
                        this.loadFolders();
                        this.lbxFolderTree.selectFirst();
                    }, this)
                });
            }, this));
            dlg.open();
        }, this));
        
        this.btnNewLinkCollection = $('#btnNewLinkCollection');
        this.btnNewLinkCollection.on('click', $.proxy(function(){
            window.location = 'NewLinkCollection';
        }, this));
        
        this.btnViewCollection = $('#btnViewLinkCollection');
        this.btnViewCollection.on('click', $.proxy(function(){
            window.location = 'ViewLinkCollection?linkcollection_id=' + this.tblLinks.getSelectedValue();
        }, this));
        
        this.btnDeleteLinkCollection = $('#btnDeleteLinkCollection');
        this.btnDeleteLinkCollection.on('click', $.proxy(function(){
            
            var dlg = new Rein.ConfirmDialog("Do you realy want to delete this LinkCollection?", $.proxy(function() {
                $.ajax({
                    url: 'DeleteLinkCollection',
                    type: 'GET',
                    data: {
                        linkcollection_id: this.tblLinks.getSelectedValue()
                    },
                    complete: $.proxy(function(response) {
                        this.loadFolderContent(this.lbxFolderTree.getSelectedValue());
                    }, this)
                });
            }, this));
            dlg.open();

        }, this));
        
        // Load folders from server
        this.loadFolders();
    }
    
    /**
     * Requests folder tree from server,
     * server returns JSON
     */
    this.loadFolders = function() {
        
        $.ajax({
            url: 'GetFolderTree',
            type: 'GET',
            success: $.proxy(function(jsonTree) {
                
                this.lbxFolderTree.clear();
                this.folders = {};
                this.displayFolder(jsonTree, 0);
                
                var folderId = $.cookie('currentFolderId');
                if (folderId != null) {
                    this.lbxFolderTree.select(folderId);
                }
                else {
                    this.lbxFolderTree.selectFirst();
                }
            }, this),
            dataType: 'json'
        });
    }
    
    /**
     * Displays the folders
     */
    this.displayFolder = function(jsonFolder, level) {
        
        this.folders[jsonFolder.id] = jsonFolder;
        this.lbxFolderTree.addItem(jsonFolder.id,
                                    jsonFolder.name,
                                    level);
        
        $.each(jsonFolder.childs, $.proxy(function(i, child){
            this.displayFolder(child, level + 1);
        }, this));
    }
    
    /**
     * Gets called when the selection in the folder tree has changed.
     * This handler will request the folder contents from the server.
     */
    this.handleFolderSelect = function() {
        
        var folder = this.folders[this.lbxFolderTree.getSelectedValue()];
        $.cookie('currentFolderId', folder.id, {path: '/'});
        
        if (folder.parentId == -1) {
            this.btnEditFolder.attr('disabled', 'disabled');
            this.btnDeleteFolder.attr('disabled', 'disabled');
        }
        else {
            this.btnEditFolder.removeAttr('disabled');
            this.btnDeleteFolder.removeAttr('disabled');
        }
        
        this.loadFolderContent(folder.id);
    }
    
    this.loadFolderContent = function(folderId) {
        $.ajax({
            url: 'GetFolderContent',
            type: 'GET',
            dataType: 'json',
            data: {
                folder_id: folderId
            },
            success: $.proxy(function(folderContent) {
                // Folder content received, display it
                this.tblLinks.clear();
                $.each(folderContent, $.proxy(function(i, linkCollection){
                    var dateCreated = new Date(linkCollection.created);
                    var sCreated = dateCreated.toDateString();
                    var dateModified = new Date(linkCollection.modified);
                    var sModified = dateModified.toDateString();
                    this.tblLinks.addRow([linkCollection.name, sCreated, sModified], linkCollection.id);
                }, this));
                this.tblLinks.selectFirst();
                
                if (this.tblLinks.getNumItems() == 0) {
                    this.btnDeleteLinkCollection.attr('disabled', 'disabled');
                    this.btnViewCollection.attr('disabled', 'disabled');
                }
                else {
                    this.btnDeleteLinkCollection.removeAttr('disabled');
                    this.btnViewCollection.removeAttr('disabled');
                }
            }, this)
        });
    }
    
    // Call init
    this.init();
}

$(document).on('ready', function() {
    new Rein.HomePage();
});